﻿using BroadGoalComponents;
using BroadGoalComponents.Dialogs;
using Custom.Framework;
using Custom.Infrastructure.EventBus;
using Custom.Model.Entity;
using Custom.Service.Repository;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Custom.Business.Sale
{
    public partial class frmSaleCustomSearch : PlatFormBase
    {
        public frmSaleCustomSearch()
        {
            InitializeComponent();

            this.bgDataGridView1.TopLeftHeaderCell.Value = "序号";
            this.bgDataGridView1.RowHeadersWidth = 55;
            this.bgDataGridView1.RowHeadersVisible = true;
            this.bgDataGridView1.ShowLineNo = true;
        }

        private void frmSaleCustomSearch_Load(object sender, EventArgs e)
        {
            this.search();
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSearch_Click(object sender, EventArgs e)
        {
            this.search();
        }

        /// <summary>
        /// 双击选择客户
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bgDataGridView1_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            int id = int.Parse(this.bgDataGridView1.CurrentRow.Cells[this.col_id.Name].Value.ToString());

            EventBus.Instance.Publish(new TEventEntity()
            {
                ActionName = "selectcustom",
                Data = id
            }, callback);

            this.Close();
        }

        /// <summary>
        /// 翻页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bgPagination_Click(object sender, EventArgs e)
        {
            this.search();
        }

        /// <summary>
        /// 页数更改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bgPagination_OnPageSizeChanged(object sender, EventArgs e)
        {
            this.bgPagination.CurrentPage = 1;
            this.search();
        }

        /// <summary>
        /// 查询
        /// </summary>
        private void search()
        {
            try
            {
                List<CustomInfo> customs = new CustomInfoRepository().GetAll().OrderByDescending(o => o.createtime).ToList();

                string condition = this.txtCondition.Text.Trim();
                if (!string.IsNullOrEmpty(condition))
                {
                    customs = customs.Where(w => w.cardno.Contains(condition) || w.identityno.Contains(condition) ||
                                            w.cusname.Contains(condition) || w.mobile.Contains(condition)).ToList();
                }
                
                // 分页控件数据绑定
                this.bgPagination.TotalPage = (customs.Count - 1) / this.bgPagination.PageSize + 1;
                if (this.bgPagination.CurrentPage >= this.bgPagination.TotalPage)
                {
                    this.bgPagination.CurrentPage = this.bgPagination.TotalPage > 0 ? this.bgPagination.TotalPage : 1;
                }

                // 绑定当前页的数据源
                this.bgDataGridView1.DataSource = customs.Skip(this.bgPagination.PageSize * (this.bgPagination.CurrentPage - 1)).Take(this.bgPagination.PageSize).ToList();
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                WarningDetailDialog.Show(this, "查询失败！", ex.Message);
            }
        }


        private void callback(TEventEntity arg1, bool arg2, Exception arg3)
        {
            
        }
    }
}
